package com.oocl.crm.service;

import java.util.Comparator;

import com.oocl.crm.datastructure.MyLinkedList;
import com.oocl.crm.exception.CrmException;
import com.oocl.crm.model.Customer;

public enum CustomerService {
	INSTANCE;

	private MyLinkedList<Customer> customerList;
	private int idAllocator;

	CustomerService() {
		customerList = new MyLinkedList<>();
		idAllocator = 0;
		
		// add 10 data
		try {
			addCustomer("Harden", "Male", "2001/02/02", "HOU", "10001");
			addCustomer("Helen", "Female", "2000/03/07", "TST", "12223");
			addCustomer("Mary", "Female", "2001/02/06", "UND", "15522");
			addCustomer("Curry", "Male", "2000/06/05", "GSW", "12272");
			addCustomer("Irving", "Male", "2000/08/02", "CLE", "18122");
			addCustomer("Katy", "Female", "2000/04/07", "MKT", "19022");
			addCustomer("Anthony", "Male", "2000/12/03", "NYK", "12782");
			addCustomer("Wall", "Male", "2000/05/03", "WAS", "12002");
			addCustomer("Russell", "Male", "2000/03/02", "OKC", "16222");
		} catch (CrmException e) {
			e.printStackTrace();
		}
	}

	public void addCustomer(String name, String sex, String birthday,
			String address, String phone) throws CrmException {
		if (name == null || name.equals("")) {
			throw new CrmException("Name cannot be empty!");
		}
		Customer customer = new Customer(Integer.toString(idAllocator++), name,
				sex, birthday, address, phone);
		customerList.insert(customerList.getSize(), customer);
	}

	public void removeCustomerById(String id) throws CrmException {
		customerList.removeById(id);
	}
	
	public void removeAllCustomers() {
		customerList.clear();
		idAllocator = 0;
	}

	public void updateCustomerById(String id, String name, String sex,
			String birthday, String address, String phone) throws CrmException {
		Customer idMatchCustomer = customerList.findById(id);
		name = (name == null || name.equals("")) ? idMatchCustomer.getName()
				: name;
		sex = (sex == null || sex.equals("")) ? idMatchCustomer.getSex() : sex;
		birthday = (birthday == null || birthday.equals("")) ? idMatchCustomer
				.getBirthday() : birthday;
		address = (address == null || address.equals("")) ? idMatchCustomer
				.getAddress() : address;
		phone = (phone == null || phone.equals("")) ? idMatchCustomer
				.getPhone() : phone;
		customerList.updateById(new Customer(id, name, sex, birthday, address,
				phone));
	}

	public String sortCustomerBy(String sortBy) {
		Comparator<Customer> comparator = null;
		switch (sortBy) {
		case "id":
			comparator = new Comparator<Customer>() {

				@Override
				public int compare(Customer c1, Customer c2) {
					return c1.getId().compareTo(c2.getId());
				}
			};
			break;
		case "name":
			comparator = new Comparator<Customer>() {

				@Override
				public int compare(Customer c1, Customer c2) {
					return c1.getName().compareTo(c2.getName());
				}
			};
			break;
		case "sex":
			comparator = new Comparator<Customer>() {

				@Override
				public int compare(Customer c1, Customer c2) {
					return c1.getSex().compareTo(c2.getSex());
				}
			};
			break;
		case "birthday":
			comparator = new Comparator<Customer>() {

				@Override
				public int compare(Customer c1, Customer c2) {
					return c1.getBirthday().compareTo(c2.getBirthday());
				}
			};
			break;
		case "address":
			comparator = new Comparator<Customer>() {

				@Override
				public int compare(Customer c1, Customer c2) {
					return c1.getAddress().compareTo(c2.getAddress());
				}
			};
			break;
		case "phone":
			comparator = new Comparator<Customer>() {

				@Override
				public int compare(Customer c1, Customer c2) {
					return c1.getPhone().compareTo(c2.getPhone());
				}
			};
			break;
		}
		
		if (comparator == null) {
			return "";
		}
		MyLinkedList<Customer> sortResult = customerList.sortBy(comparator);
		return sortResult.getListAsString();
	}

	public String findAllCustomers() {
		return customerList.getListAsString();
	}

	public String findCustomers(String id, String name, String sex,
			String birthday, String address, String phone) throws CrmException {
		String customerListAsString = "";
		for (int i = 0; i < customerList.getSize(); i++) {
			Customer c = customerList.elementAt(i);

			boolean idEqual = (id == null || id.equals(c.getId()));
			boolean nameEqual = (name == null || name.equals(c.getName()));
			boolean sexEqual = (sex == null || sex.equals(c.getSex()));
			boolean birthdayEqual = (birthday == null || birthday.equals(c
					.getBirthday()));
			boolean addressEqual = (address == null || address.equals(c
					.getAddress()));
			boolean phoneEqual = (phone == null || phone.equals(c.getPhone()));

			if (idEqual && nameEqual && sexEqual && birthdayEqual
					&& addressEqual && phoneEqual) {
				customerListAsString += c.toString() + "\n";
			}
		}
		return customerListAsString;
	}
}
